package com.wy.sloth.receiver.config;

import java.net.UnknownHostException;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import com.alibaba.fastjson.JSONObject;
import com.wy.sloth.config.SlothConfig;
import com.wy.sloth.core.SlothSender;
import com.wy.sloth.core.SlothStandardReceiver;
import com.wy.sloth.core.SlothStandardSender;
import com.wy.sloth.receiver.handler.Test2Handler;
import com.wy.sloth.receiver.handler.TestHandler;
import com.wy.sloth.receiver.handler.TestSyncHandler;
import com.wy.sloth.redis.service.SlothRedisService;
import com.wy.sloth.util.NetUtils;

/**
 * 
 * 
 * @author wangyuan
 * @since 2019年2月14日
 */
@Configuration
public class MsgQueueConfig {
    
    @Resource(name = "redisTemplate")
    private RedisTemplate<String, JSONObject> redisTemplate;
    
    @Resource(name = "redisTemplate")
    private RedisTemplate<String, String> stringRedisTemplate;
    
    @Bean
    public SlothConfig slothConfig(ConfigurableApplicationContext context) throws UnknownHostException {
        SlothConfig config = new SlothConfig();
        
        config.setServiceIp(NetUtils.getHostAddress());
        config.setServicePort(Integer.valueOf(context.getEnvironment().getProperty("server.port")));
        config.setServiceName(context.getId());
        //config.setGroup("1111111");
        
        return config;
    }

    @Bean
    public SlothStandardReceiver slothReceiver(SlothConfig config, 
            TestHandler testHandler, Test2Handler test2Handler, TestSyncHandler testSyncHandler) {
        SlothStandardReceiver slothReceiver = new SlothStandardReceiver(redisTemplate, stringRedisTemplate, config);
        
        slothReceiver.setAutoRegisterTypeAndQueueRelation(true);
        
        //注册消息处理器
        slothReceiver.registerHandler("1", testHandler);
        slothReceiver.registerHandler("2", test2Handler);
        slothReceiver.registerHandler("3", testSyncHandler);
        
        return slothReceiver;
    }
    
    @Bean
    public SlothSender slothSender(SlothConfig config) throws UnknownHostException {
        SlothStandardSender slothReceiver = new SlothStandardSender(redisTemplate, stringRedisTemplate, config);
        return slothReceiver;
    }
    
    @Bean
    public SlothRedisService slothRedisService(SlothConfig config) {
        return new SlothRedisService(redisTemplate, stringRedisTemplate, config);
    }
    
    @Autowired
    private SlothStandardReceiver slothReceiver;
    
    @PostConstruct
    public void init() {
        slothReceiver.start();
    }
    
    @PreDestroy
    public void destory() {
        slothReceiver.destroy();
    }
}
